package de.klingbeil.hhc.client.handler;

import java.util.List;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.web.bindery.requestfactory.shared.Receiver;

import de.klingbeil.hhc.client.ItemProxy;
import de.klingbeil.hhc.client.ItemRequestFactory.ItemRequestContext;

public class ItemDataHandler {
	public static abstract class ItemReceiver extends Receiver<List<ItemProxy>> {
	}

	private final Provider<ItemRequestContext> provider;

	@Inject
	public ItemDataHandler(Provider<ItemRequestContext> provider) {
		this.provider = provider;
	}

	public void findAll(Receiver<List<ItemProxy>> receiver) {
		provider.get().findAll().fire(receiver);
	}

	public void findById(Long id, Receiver<ItemProxy> receiver) {
		provider.get().findById(id).fire(receiver);
	}

	public void persist(String description, Receiver<ItemProxy> receiver) {
		ItemRequestContext itemRequestContext = provider.get();
		ItemProxy item = itemRequestContext.create(ItemProxy.class);
		item.setDescription(description);
		itemRequestContext.persist(item).fire(receiver);
	}

}
